package myServlet;

import Smt.ServerImp.BillServerImp;
import Smt.Util.Bill;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@SuppressWarnings("serial")
public class BillAdd extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String ids = request.getParameter("iid");
		int id = 0;
		if (ids != null) {
			id = Integer.parseInt(ids);
		}
		String prices = request.getParameter("iprice");
		double price = 0;
		if (prices != null) {
			price = Double.parseDouble(prices);
		}
		String unit = request.getParameter("iunit");
		String nums = request.getParameter("inumber");
		int number = 0;
		if (nums != null) {
			number = Integer.parseInt(nums);
		}
		String description = request.getParameter("idescription");
		String pay = request.getParameter("ipay");
		String name = request.getParameter("iname");
		int sno = Integer.parseInt(request.getParameter("is_name"));
		Bill b = new Bill(id, price, unit, number, name, description, sno, pay);
		BillServerImp bsi = new BillServerImp();
		if (bsi.add(b)) {
			response.sendRedirect("BillRead");
		} else {
			request.setAttribute("error", "添加失败,我也不知道是神马问题！");
			request.getRequestDispatcher("BillRead").forward(request, response);
		}

	}

}
